package com.google.code.c2dmtester;

import android.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

public class C2dmReceiver extends BroadcastReceiver
{
    public final static int NOTIFICATION_ID = 123; // any number will do

    @Override
    public void onReceive( Context context, Intent intent )
    {
        if( intent.getAction().equals(
                "com.google.android.c2dm.intent.REGISTRATION" ) )
        {
            String registration = intent.getStringExtra( "registration_id" );
            Log.i( "regid", (null == registration) ? "(null)" : registration );

            if( intent.getStringExtra( "error" ) != null )
            {
                // handle an error for registration
                String title = "error receiving registration ID";
                String message = registration;
                Intent myIntent = new Intent( context, MainActivity.class );
                createNotification( context, title, message, myIntent );
            }
            else if( intent.getStringExtra( "unregistered" ) != null )
            {
                // call service to handle unregistration
                String title = "unregistering event received";
                String message = registration;
                Intent myIntent = new Intent( context, MainActivity.class );
                createNotification( context, title, message, myIntent );
            }
            else if( registration != null )
            {
                // call service to handle registration
                Intent myIntent = new Intent( context, MainActivity.class );
                myIntent.putExtra( MainActivity.REGISTRATION_ID,
                        registration );
                // String title = "registration ID received";
                // String message = registration;
                // createNotification(context, title, message, myIntent);

                // open activity as new task (and close older running instance
                // this needs to be done, if a Broadcast Receiver wants to start
                // an activity
                myIntent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
                myIntent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
                context.startActivity( myIntent );
                Log.i( "C2D RegID:", registration );

            }
        }
        else if( intent.getAction().equals(
                "com.google.android.c2dm.intent.RECEIVE" ) )
        {
            // we are just going to say a few fields already exist here, but
            // this is based
            // on what you may be sending from above in your data fields
            String title = intent.getExtras().getString( "title" );
            String message = intent.getExtras().getString( "text" );
            String url = intent.getExtras().getString( "url" );

            Intent myIntent = new Intent( context, MainActivity.class );
            myIntent.putExtra( MainActivity.URL, url );
            myIntent.setData( Uri.parse( url ) );
            createNotification( context, title, message, myIntent );
        }
    }

    private void createNotification( Context context, String title,
            String message, Intent myIntent )
    {
        PendingIntent contentIntent = PendingIntent.getActivity( context, 0,
                myIntent, PendingIntent.FLAG_UPDATE_CURRENT );

        // create the notification!
        int icon = R.drawable.stat_notify_chat;
        long when = System.currentTimeMillis();
        CharSequence contentTitle, contentMessage = null;
        contentTitle = (CharSequence) title;

        contentMessage = (CharSequence) message;

        NotificationManager nm = (NotificationManager) context
                .getSystemService( Context.NOTIFICATION_SERVICE );
        Notification n = new Notification( icon, contentTitle, when );
        n.setLatestEventInfo( context, contentTitle, contentMessage,
                contentIntent );
        nm.notify( C2dmReceiver.NOTIFICATION_ID, n ); // 0 can be replaced with a
                                                      // number for separate style
                                                      // of messages
    }
}
